package cn.net.lwj.common.control;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.ServletContextAware;

//@Component(value="fzbabaseControl")
public class BaseControl implements ServletContextAware {
    //使用import org.apache.log4j.Logger;的方式
    //public Logger log = Logger.getLogger(this.getClass());
    protected static Logger log = LoggerFactory.getLogger(BaseControl.class);

    protected HttpSession session;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected ServletContext servletContext;

    public String getRequestValue(String name) {
        return getRequest().getParameter(name) == null || "".equals(getRequest().getParameter(name).trim()) ? "" : getRequest().getParameter(name).trim();
    }

    public String[] getRequestValues(String name) {
        return getRequest().getParameterValues(name) == null || "".equals(getRequest().getParameterValues(name)) ? new String[0] : getRequest().getParameterValues(name);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    /**
     * 对servletAPI进行赋值  这个方法必须要
     * 被@ModelAttribute注释的方法会在此controller每个方法执行前被执行
     *
     * @param req
     * @param rep
     * @param session
     * @author
     */
    @ModelAttribute
    public void initServletAPI(HttpServletRequest req, HttpServletResponse rep, HttpSession session) {
        this.request = req;
        this.response = rep;
        this.session = session;
    }

    public String getStr(Object obj) {
        return obj == null || "underfined".equals(obj.toString()) || "".equals(obj.toString()) || "null".equals(obj.toString()) ? "" : obj.toString();
    }

    public Integer getInt(Object obj) {
        return obj == null || "".equals(obj.toString()) || "null".equals(obj.toString()) ? 0 : Integer.parseInt(obj.toString());
    }

}
